﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class 药品出库单录入 : Form
    {
        public static int ckdh = 0;//出库单号
        public int c0_flag = 0;//出库单flag
        public bool is_new = true;//是否为新单的标志
        public string ksbm = "";//科室编码
        public int xh = 0;//序号

        public 药品出库单录入(int dh)
        {
            ckdh = dh;
            InitializeComponent();
            fresh();
        }

        private void button1_Click(object sender, EventArgs e)//保存
        {
            string dtp1 = dateTimePicker1.Value.ToShortDateString();
            int dpt_1 = 0;
            int cklx = 0;//出库类型
            c0_flag = 16;
            string cmdtext6 = "select  convert(int,(select Convert(DateTime,'" + dtp1 + "')))+2";
            string cmdtext7 = "select bm from yk_crlx where mc='" + comboBox1.Text.ToString() + "'";
            SqlConnection cn1 = new SqlConnection(SQL.conStr);//出库日期
            {
                cn1.Open();
                SqlCommand com = new SqlCommand(cmdtext6, cn1);
                SqlDataReader read = com.ExecuteReader();
                if (read.Read())
                {
                    dpt_1 = (int)read[0];
                }
                read.Close();
                cn1.Close();
            }

            SqlConnection cn2 = new SqlConnection(SQL.conStr);//出库类型
            {
                cn2.Open();
                SqlCommand com = new SqlCommand(cmdtext7, cn2);
                SqlDataReader read = com.ExecuteReader();
                if (read.Read())
                {
                    cklx = System.Convert.ToInt32(read[0]);
                }
                read.Close();
                cn2.Close();
            }

            SqlConnection cn5 = new SqlConnection(SQL.conStr);//科室编码
            {
                cn5.Open();
                string cmdtext5 = "SELECT bm as 编码   from zd_ks where  mc= '" + textBox13.Text + "'";
                SqlCommand com = new SqlCommand(cmdtext5, cn5);
                SqlDataReader read5 = com.ExecuteReader();
                if (read5.Read())
                {
                    ksbm = read5["编码"].ToString();
                }
                read5.Close();
            }
            if (is_new)
            {
                string cmdtext3 = " INSERT INTO YK_CKD0(XH,YKBM,CKDH,RQ,SJ,CKLX,JLLX,KSBM,SHRY,JZRY,FLAG,BZ,JZRQ,SQDH,LYR,lrry)" +
                                  "  VALUES(" + xh + ", '401', " + textBox1.Text + ", " + dpt_1 + ", 60914, " + cklx + ", 1, '" + ksbm + "', '', '', 16, '" + textBox8.Text + "', 0, '', '" + textBox12.Text + "', '" + LoginForm.username + "') ";
                SqlConnection cn3 = new SqlConnection(SQL.conStr);
                {
                    cn3.Open();
                    SqlCommand com = new SqlCommand(cmdtext3, cn3);
                    com.ExecuteNonQuery();
                }

            }
            else
            {
                string cmdtext3 = " update YK_CKD0 set RQ=" + dpt_1 + ", CKLX=" + cklx + "  ,KSBM='" + ksbm + "' ,BZ='" + textBox8.Text + "'  ,LYR='" + textBox12.Text + "',lrry='" + LoginForm.username + "'  where ckdh= " + ckdh;
                SqlConnection cn4 = new SqlConnection(SQL.conStr);
                {
                    cn4.Open();
                    SqlCommand com = new SqlCommand(cmdtext3, cn4);
                    com.ExecuteNonQuery();
                }
            }
            is_new = false;
            MessageBox.Show("保存成功");
        }

        private void button5_Click(object sender, EventArgs e)//消审
        {
            SqlConnection cn = new SqlConnection(SQL.conStr);
            cn.Open();
            string cmdtext3 = "   BEGIN TRANSACTION  " +
                              "UPDATE YK_CKD0 SET " +
                              "SHRY = '', " +
                              "JZRY = '', " +
                              "FLAG = 16  " +
                              "WHERE ckdh = " + ckdh +
                              "   COMMIT TRANSACTION ";
            SqlCommand cmd = new SqlCommand(cmdtext3, cn);
            cmd.ExecuteNonQuery();
            fresh();
        }

        private void button6_Click(object sender, EventArgs e)//记账
        {
            int jzrq = 0;//记账日期
            SqlConnection cn1 = new SqlConnection(SQL.conStr);
            {
                cn1.Open();
                string cmdtext4 = "select  convert(int,(select Convert(DateTime,getdate())))+1";
                SqlCommand cmd = new SqlCommand(cmdtext4, cn1);
                jzrq = (int)cmd.ExecuteScalar();
            }
            SqlConnection cn2 = new SqlConnection(SQL.conStr);
            {
                cn2.Open();
                string cmdtext3 = "BEGIN TRANSACTION  " +

                                  "UPDATE YK_CKD0 SET " +
                                  "JZRY = '"+LoginForm.username+"', " +
                                  "FLAG = 19,  " +
                                  "jzrq =" + jzrq +
                                  "  WHERE ckdh = " + ckdh +

                                  "     update yk_rkd1 set  yk_rkd1.sysl=yk_rkd1.sysl-c1.sl    from yk_ckd1 c1  " +
                                  "where c1.ckdh="+ckdh+"  and yk_rkd1.autorkid=c1.autorkid  and yk_rkd1.ypbm=c1.ypbm     "+

                                  "   COMMIT TRANSACTION ";
                //陈立辉 加的
                string cmd_insert = "INSERT INTO YP_CRKJZ SELECT YPBM,-SL FROM YK_CKD1 WHERE ckdh=" + ckdh;
                SqlCommand cmd1 = new SqlCommand(cmd_insert, cn2);
                cmd1.ExecuteNonQuery();
                //
                SqlCommand cmd = new SqlCommand(cmdtext3, cn2);
                cmd.ExecuteNonQuery();
            }
            MessageBox.Show("记账成功");
            fresh();

        }

        private void button2_Click(object sender, EventArgs e)//审核
        {
            SqlConnection cn = new SqlConnection(SQL.conStr);
            cn.Open();
            string cmdtext3 = "   BEGIN TRANSACTION  " +
                              "UPDATE YK_CKD0 SET " +
                              "SHRY = '"+LoginForm.username+"', " +
                              "JZRY = '', " +
                              "FLAG = 17  " +
                              "WHERE ckdh = " + ckdh +
                              "   COMMIT TRANSACTION ";
            SqlCommand cmd = new SqlCommand(cmdtext3, cn);
            cmd.ExecuteNonQuery();
            fresh();
        }

        public void fresh()//刷新
        {
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                string cmdtext1 = "select  ckdh as 出库单号,xh as 序号,(select  convert(datetime,rq-2)) as 制单日期,CR.MC AS 出库类型,ks.mc as '领用科室/退药厂商'," +
                                "lrry as 录入,shry as 审核,jzry as 记账,bz 备注, c0.flag as 状态,c0.lyr as 领用人  FROM YK_CKD0 C0 LEFT JOIN YK_CRLX CR ON C0.CKLX = CR.BM LEFT JOIN zd_ks ks ON C0.ksbm = ks.BM   " +
                                "WHERE C0.ckdh= " + ckdh;
                SqlCommand com = new SqlCommand(cmdtext1, cn);
                SqlDataReader dr = com.ExecuteReader();
                textBox1.Text = ckdh.ToString();
                if (dr.Read())
                {

                    comboBox1.Text = dr["出库类型"].ToString();
                    textBox12.Text = dr["领用人"].ToString();
                    dateTimePicker1.Text = dr["制单日期"].ToString();
                    textBox13.Text = dr["领用科室/退药厂商"].ToString();
                    textBox6.Text = dr["序号"].ToString();
                    textBox7.Text = dr["录入"].ToString();
                    textBox9.Text = dr["审核"].ToString();
                    textBox10.Text = dr["记账"].ToString();
                    textBox8.Text = dr["备注"].ToString();
                    c0_flag = (int)dr["状态"];
                    is_new = false;
                    this.dataGridView1.AllowUserToAddRows = false;

                }
                else
                {
                    this.dataGridView1.AllowUserToAddRows = true;
                    is_new = true;
                }
                dr.Close();
                string cmdtext2 = "SELECT C1.ypbm 编码,YP.MC AS 名称,YP.GG as 规格,yp.bzdw as 单位," +
                                   " c1.ncsl as 拟出数,sum(C1.SL) as  实出数,sum(C1.cbje) as 进价金额,C1.pjje as  批价金额,sum(C1.je) as  零价金额,C1.bz as  备注      " +
                                   "FROM YK_CKD1 C1  " +
                                   "   LEFT JOIN ZD_YP YP ON C1.YPBM = YP.BM" +
                                    "  WHERE C1.ckdh =" + ckdh +
                                    "  group by C1.YPBM,yp.mc,yp.gg,yp.bzdw,c1.ncsl,C1.pjje,C1.bz  ";
                SqlDataAdapter da = new SqlDataAdapter(cmdtext2, cn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }

            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.MultiSelect = false;


            if (!is_new)
            {
                textBox2.Text = dataGridView1.Rows.Count.ToString();
                decimal textBox3_Text = 0;
                decimal textBox4_Text = 0;
                decimal textBox5_Text = 0;
                for (int index = 0; index < dataGridView1.Rows.Count; index++)
                {
                    textBox3_Text = (decimal)dataGridView1[6, index].Value + textBox3_Text;
                    //textBox4_Text = (decimal)dataGridView1[7, index].Value + textBox4_Text;
                    textBox5_Text = (decimal)dataGridView1[8, index].Value + textBox5_Text;
                }
                textBox3.Text = textBox3_Text.ToString();
                textBox4.Text = textBox4_Text.ToString();
                textBox5.Text = textBox5_Text.ToString();
            }
            selectRow();
        }

        public void selectRow()
        {
            if (c0_flag == 19)
            {
                button1.Enabled = false;
                button2.Enabled = false;
                button5.Enabled = false;
                button6.Enabled = false;

            }
            else if (c0_flag == 17)
            {
                button1.Enabled = false;
                button2.Enabled = false;
                button5.Enabled = true;
                button6.Enabled = true;

            }
            else if (c0_flag == 16)
            {
                button1.Enabled = true;
                button2.Enabled = true;
                button5.Enabled = false;
                button6.Enabled = false;

            }
        }

        public void select_bm()
        {
            string cmdtext5 = "SELECT bm as 编码,MC AS 名称  from zd_ks where  dh = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' or bm = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' or mc = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' ";
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlCommand com = new SqlCommand(cmdtext5, cn);
                SqlDataReader read = com.ExecuteReader();
                if (read.Read())
                {
                    textBox13.Text = read["名称"].ToString();
                    ksbm = read["编码"].ToString();
                }
                read.Close();
            }
            dataGridView2.Visible = false;
        }


        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            Form f = new 出库单编辑(ckdh, c0_flag);
            f.ShowDialog();
            fresh();
            c0_flag = 16;
        }

        private void 药品出库单录入_Load(object sender, EventArgs e)
        {
            dataGridView2.Visible = false;
            this.dataGridView2.AllowUserToAddRows = false;
            this.dataGridView2.MultiSelect = false;
            this.dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView2.ReadOnly = true;

            try
            {

                string cmdtext1 = "select xh from  yk_ckd0  order by xh desc ";
                SqlConnection cn = new SqlConnection(SQL.conStr);
                {
                    cn.Open();
                    SqlCommand com = new SqlCommand(cmdtext1, cn);
                    xh = (int)com.ExecuteScalar() + 1;
                }
                textBox6.Text = xh.ToString();
            }
            catch  { return; } 
        }

        private void textBox13_KeyPress(object sender, KeyPressEventArgs e)//申请药房press
        {
            string cmdtext4 = "select bm 编码,MC AS 名称 from zd_ks where dh like '" + textBox13.Text.Trim() + "%' or bm like '" + textBox13.Text.Trim() + "%' or mc like '" + textBox13.Text.Trim() + "%'";
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlDataAdapter da1 = new SqlDataAdapter(cmdtext4, cn);
                DataTable dt = new DataTable();
                da1.Fill(dt);
                dataGridView2.DataSource = dt;
                dataGridView2.Visible = true;
                dataGridView2.Columns[0].Width = 60;
                dataGridView2.Columns[1].Width = 150;
            }

        }

        private void textBox13_MouseClick(object sender, MouseEventArgs e)
        {
            dataGridView2.Visible = false;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView2_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            select_bm();
        }
    }
}